# -*- coding: UTF-8 -*-
import panda3d.core as CORE
class GameCamera:
    def __init__(self,game,role):
        self.Camera = base.camera
        self.game_obj = game
        self.role = role
        self.view_angle = 8
        return
    def initCamera(self):
        self.floater = CORE.NodePath(CORE.PandaNode("floater"))
        self.floater.reparentTo(render)
        
        base.disableMouse()
        
        self.Camera.setPos(self.role.ralph.getX(),self.role.ralph.getY()+10,self.view_angle)
        self.Camera.lookAt(self.role.ralph)
        #设置摄像机的碰撞射线
        self.camGroundRay = CORE.CollisionRay()
        self.camGroundRay.setOrigin(0,0,1000)
        self.camGroundRay.setDirection(0,0,-1)
        self.camGroundCol = CORE.CollisionNode('camRay')
        self.camGroundCol.addSolid(self.camGroundRay)
        self.camGroundCol.setFromCollideMask(CORE.BitMask32.bit(0))
        self.camGroundCol.setIntoCollideMask(CORE.BitMask32.allOff())
        self.camGroundColNp = self.Camera.attachNewNode(self.camGroundCol)
        self.camGroundHandler = CORE.CollisionHandlerQueue()
        self.game_obj.CTrav.addCollider(self.camGroundColNp, self.camGroundHandler)
    
    def moveCamLeft(self):
        self.Camera.setX(self.Camera, 20 * globalClock.getDt())
    def moveCamRight(self):
        self.Camera.setX(self.Camera, -20 * globalClock.getDt())
    def setCamPos(self,pos):
        self.Camera.setPos(pos)
    def getCamPos(self):
        return self.Camera.getPos()
    
    def reverseCam(self):
        camvec = self.role.ralph.getPos() - self.Camera.getPos()
        camvec.setZ(self.view_angle)
        camdist = camvec.length()
        camvec.normalize()
        if (camdist > 10.0):
            self.setCamPos(self.getCamPos() + camvec*(camdist-10))
            camdist = 10.0
        if (camdist < 5.0):
            self.setCamPos(self.getCamPos()- camvec*(5-camdist))
            camdist = 5.0
    
    def resetCam(self):
        self.Camera.setX(self.role.ralph.getX())
        self.Camera.setY(self.role.ralph.getY()+15)
        self.Camera.lookAt(self.role.ralph)
